<template>
  <div>
    <el-table v-loading="loading" :data="dataSource" border stripe :height="autoHeight">
      <el-table-column label="计算名称" prop="computeName" width="150px" align="center"></el-table-column>
      <el-table-column label="计算属性" prop="computeProp" width="150px" align="center"></el-table-column>
      <el-table-column label="计算脚本" prop="computeScript" show-overflow-tooltip></el-table-column>
      <el-table-column label="操作" width="100px" align="center">
        <template slot="header">
          <el-button type="primary" @click="visible = true" icon="el-icon-circle-plus-outline"></el-button>
        </template>
        <template slot-scope="scope">
          <el-button type="success" icon="el-icon-edit" circle @click="openEdit(scope.row)" title="编辑"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="removeData(scope.row, scope.$index)" title="删除"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="lab-table-page">
      <div style="display: inline-flex">
        <el-pagination
            :disabled="loading"
            :current-page="pageInfo.page"
            :page-sizes="[5, 10, 15, 20, 50, 100]"
            :page-size="pageInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageInfo.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
        <el-button-group style="padding: 2px 5px">
          <el-button icon="el-icon-refresh" title="刷新" :disabled="loading" @click="loadSource([])" />
        </el-button-group>
      </div>
    </div>

    <el-dialog title="输出配置" :visible.sync="visible" width="80%">
      <el-form label-width="80px" label-position="right">
        <el-form-item label="计算名称" required>
          <el-input v-model="entityInfo.computeName"></el-input>
        </el-form-item>
        <el-form-item label="计算属性" required>
          <el-input v-model="entityInfo.computeProp"></el-input>
        </el-form-item>
        <el-form-item label="计算顺序">
          <el-input-number v-model="entityInfo.computeOrder" style="width: 100%" controls-position="right"></el-input-number>
        </el-form-item>
        <el-form-item label="计算脚本">
          <code-editor ref="mapperRef" :config="{language:'java'}" style="height: 500px"></code-editor>
        </el-form-item>
        <el-form-item style="text-align: center">
          <el-button icon="el-icon-circle-check" type="success" @click="saveOut">{{ entityInfo.computeId ? '更新' : '保存' }}</el-button>
          <el-button icon="el-icon-circle-close" type="info" @click="visible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import IdeaTable from "@/mixins/IdeaTable";
import CodeEditor from "@/components/codeEditor/CodeEditor";

export default {
  name: "ExcelComputeTable",
  components: {CodeEditor},
  mixins: [IdeaTable],
  inject: {
    excelTemplate: {
      from: 'excelTemplate',
      default: function () {
        return { sheets: [], excelId: '0', excelOut: {} }
      }
    }
  },
  computed: {
    autoHeight(){
      return `calc(100vh - 292px)`
    }
  },
  created() {
    this.queryUrl = 'excelCompute/queryPage'
    this.saveUrl = 'excelCompute/save'
    this.removeUrl = 'excelCompute/delete/'
    this.rowKey = 'computeId'
    this.addFilter({ field: 'outId', op: '=', value: this.excelTemplate.excelOut.outId})
    this.pageInfo.sorts = [{field: 'computeOrder', asc: true }]
    this.loadSource()
  },
  methods: {
    initEntity() {
      this.entityInfo = { computeQuery: '', computeScript: ''}
      this.apdateRef(true)
    },
    openEdit(row) {
      this.entityInfo = this.$Util.copy(row)
      this.visible = true
      setTimeout(() => {
        this.apdateRef(true)
      }, 500)
    },
    apdateRef(set) {
      const $mapper = this.$refs.mapperRef
      if (set) {
        $mapper.setValue(this.entityInfo.computeScript, 'java')
      } else {
        this.$set(this.entityInfo, 'computeScript', $mapper.code)
        this.$set(this.entityInfo, 'outId', this.excelTemplate.excelOut.outId)
        this.$set(this.entityInfo, 'excelId', this.excelTemplate.excelId)
      }
    },
    saveOut() {
      this.apdateRef(false)
      this.saveData(this.entityInfo)
    }
  }
}
</script>

<style scoped>

</style>
